<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogVisible" :before-close="changedialogVisible">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="addNote">新增</el-button>
        </el-col>

        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="delNote"
          >删除</el-button>
        </el-col>
        <div
          style="float:right;margin-right:10px;margin-top:5px;cursor: pointer;   font-weight: 600;"
        >
          <i class="iconfont iconhelp" @click="preview"></i>
        </div>
      </el-row>
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        v-loading="loading"
        @selection-change="handleSelectionChange"
        @sort-change="tableSortChange"
        :default-sort="{prop: 'createTime', order: 'descending'}"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column
          prop="content"
          label="便签内容"
          :show-overflow-tooltip="true"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        ></el-table-column>
        <el-table-column
          prop="projectName"
          label="关联项目"
          :show-overflow-tooltip="true"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        ></el-table-column>
        <el-table-column
          prop="businessName"
          label="关联业务"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        ></el-table-column>

        <el-table-column label="关联干系人" align="center" width="150">
          <template slot-scope="scope">
            <span>{{getStakeholderName(scope.row.stakeholderList)}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="200"
          align="center"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        ></el-table-column>
        <el-table-column label="操作" width="80" align="center">
          <template slot-scope="scope">
            <a @click="handleEdit(scope.$index, scope.row)">
              <i class="el-icon-edit aicon" />编辑
            </a>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-dialog>
    <project-note
      :dialogVisible="editVisible"
      @changedialogVisible="changeeditVisible"
      :isProject="isProject"
      :noteId="noteId"
      :isEdit="isEdit"
      :stakeholderIdNow="stakeholderId"
      :businessIdNow="businessId"
      v-if="editVisible"
    ></project-note>
    <!-- 预览 -->
    <el-dialog :visible.sync="openPreview" width="80%">
      <iframe :src="manualPreviewUrl" frameborder="0" width="100%" height="700px"></iframe>
    </el-dialog>
  </div>
</template>
<script>
import { getNoteList, delNote } from '@/api/note'
import projectNote from '@/components/Note/projectNote'
export default {
  name: 'NoteList',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    isProject: {
      type: Boolean,
      default: false
    },
    stakeholderId: {
      type: String,
      default: null
    },
    businessId: {
      type: String,
      default: undefined
    }
  },
  components: {
    projectNote
  },
  data () {
    return {
      manualPreviewUrl: null,
      openPreview: false,
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        stakeholderId: undefined,
        projectId: undefined
      },
      isAdd: false,
      isEdit: false,
      total: 0,
      noteId: null,
      loading: true,
      editVisible: false,
      tableData: [],
      multipleSelection: [],
      multiple: true
    }
  },
  // watch: {
  //   'noteListId' (val, oldVal) {
  //     if (val !== null) {
  //       if (!this.isProject) {
  //         this.queryParams.stakeholderId = val
  //       }
  //       else {
  //         this.queryParams.stakeholderId = undefined
  //       }
  //       this.getList()
  //     }
  //   }
  // },
  mounted () {
    console.log(this.stakeholderId)
    this.queryParams.businessId = this.businessId
    this.queryParams.stakeholderId = this.stakeholderId
    this.getList()
  },
  methods: {
    preview () {
      this.openPreview = true;
      this.manualPreviewUrl = `${this.previewUrl}${encodeURIComponent(
        `${this.operationManual}项目主页/业务系统操作说明-项目主页-便签.xlsx`
      )}&name=${encodeURIComponent("业务系统操作说明-项目主页-便签.xlsx")}`;
    },
    getList () {
      this.loading = true
      if (this.$route.params.projectId) {
        this.queryParams.projectId = this.$route.params.projectId
      } else {
        this.queryParams.projectId = this.$route.params.id
      }

      getNoteList(this.queryParams).then(res => {
        if (res.code === 200) {
          this.tableData = res.rows
          this.loading = false
          this.total = res.total
        }
      })
    },
    changedialogVisible () {
      this.$emit('changedialogVisible', false)
    },
    handleSelectionChange (val) {
      this.multiple = !val.length
      this.multipleSelection = val.map(item => item.guid)
    },
    addNote () {
      // this.isAdd = true
      this.isEdit = false
      this.noteId = null
      this.editVisible = true
    },
    handleEdit (index, row) {
      // this.isAdd = false
      this.isEdit = true
      this.noteId = row.guid
      this.editVisible = true
    },
    delNote () {
      console.log(this.multipleSelection)
      this.$confirm('是否确认删除便签?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {

          return delNote(this.multipleSelection);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    changeeditVisible (val) {
      this.getList()
      this.editVisible = val
    },
    getStakeholderName (rows) {
      let getStakeholderName = ''
      rows.map((item, index) => {
        if (index === rows.length - 1) {
          getStakeholderName += item.stakeholderName || ''
        }
        else {
          getStakeholderName += item.stakeholderName + ","
        }
      })
      return getStakeholderName
    },
    tableSortChange (column) {
      this.queryParams.pageNum = 1;
      if (column.order === "descending") {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "desc";
      } else {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "asc";
      }
      this.getList();
    }
  }
}
</script>
<style lang="scss" scoped>
/deep/.el-dialog {
  width: 1200px;
  .el-dialog__header {
    height: 56px;
    background-image: linear-gradient(180deg, #bed6fa 0%, #e5edf3 100%);
    box-shadow: 0 1px 0 0 #e8e8e8;
    padding: 16px 24px;
    .el-dialog__title {
      font-family: Microsoft YaHei;
      font-size: 16px;
      color: rgba(0, 0, 0, 0.85);
      letter-spacing: 0;
      line-height: 24px;
    }
  }

  .el-table .cell {
    a {
      color: blue;
    }
  }
}
/deep/.el-dialog__body {
  padding-top: 10px;
}
</style>